using UnityEngine;
using System.Collections;

public class LevelCreationScript : MonoBehaviour {

	private Controllers controllerScript;
	private int[] levelState;
	private bool levelChanged = false;
	public Transform[] platforms;
	
	
	void Start () {
		controllerScript = (Controllers)gameObject.GetComponent("Controllers");
		levelState = new int[8];
		for(int i = 0; i < 8; i++)
			levelState[i] = 0;
	}
	

	void FixedUpdate () { 
		for( int i = 0; i < 8; i++)
		{
			if(controllerScript.pos[i] != levelState[i])
				levelChanged = true;
		}
		if(levelChanged)
		{
			for(int i = 0; i < 8; i++)
			{
				if(controllerScript.pos[i] != levelState[i])
				{
					GameObject col = GameObject.FindWithTag("col" + i);
					if(col != null)
					{
						if(controllerScript.pos[i] != 0)
						{
							GameObject plat = GameObject.Instantiate(platforms[ controllerScript.pos[i] - 1 ].gameObject, col.transform.position, Quaternion.identity) as GameObject;
							plat.transform.parent = col.transform;
							plat.transform.Translate(0, (controllerScript.pos[i] - 1) * 10 + 10, 0);
						}
						
						else
							col.transform.DetachChildren();
					}
				}
			}
			levelChanged = false;
			for(int k = 0; k < 8; k++)
				levelState[k] = controllerScript.pos[k];
		}
	}
}